﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using LyyCMS.Areas.Dash.Models;
using LyyCommon;
using LyyCMS.Core;
using Microsoft.Extensions.Logging;
using Microsoft.AspNetCore.Http;

namespace LyyCMS.Web.Areas.Dash.Controllers
{
    /// <summary>
    /// 用户 登录
    /// </summary>
    [Area("Dash")]
    [Route("Dash/[controller]/[action]")]
    public class UserController : Controller
    {
        protected readonly ILogger<BaseController> _logger;
        protected readonly LyyCMSEntities _context;
        public UserController(ILogger<BaseController> loggger, LyyCMS.Core.LyyCMSEntities lyyCMSEntities)
        {
            _logger = loggger;
            _context = lyyCMSEntities;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult Index()
        {
            LoginModel loginModel = new LoginModel()
            {
                UserName = "",
                PassWord = "",
                RemberMe = true
            };
            
            return View(loginModel);
        }


        [HttpPost]
        [ValidateAntiForgeryToken]
        public  IActionResult Login([Bind("UserName,PassWord,RemberMe")] LoginModel loginModel)
        {
            if (ModelState.IsValid)
            {
                string secertPass = LyyCommon.Security.Get32MD5One(loginModel.PassWord);
                _logger.LogInformation("登录用户 加密后的密码：" + secertPass + ",加密前的密码：" + loginModel.PassWord);

                var user = _context.Users.FirstOrDefault(x => x.UserName == loginModel.UserName && x.UserPass == secertPass);
                if (user == null)
                {
                    ModelState.AddModelError("", $"无法登录,账号或密码错误");
                    //return View();
                 
                    return RedirectToAction("Index", new { LoginModel = loginModel, saveChangesError = true });
                }
                else
                {
                    #region 将登录成功后的用户信息保存到session
                    var id = System.Text.Encoding.UTF8.GetBytes(user.Id.ToString());
                    var name = System.Text.Encoding.UTF8.GetBytes(user.UserName);
                    //var pass = System.Text.Encoding.UTF8.GetBytes(user.UserPass);
                    //HttpContext.Session.Set(Service.LyyCMSConsts.USER_ID, id);
                    //HttpContext.Session.Set(Service.LyyCMSConsts.ADMIN_NAME, name);

                    HttpContext.Session.SetString(Service.LyyCMSConsts.ADMIN_NAME, user.UserName);
                    HttpContext.Session.SetInt32(Service.LyyCMSConsts.USER_ID, user.Id);
                    //HttpContext.Session.Set(Service.LyyCMSConsts.ADMIN_PASS, pass);
                    #endregion


                    int? uid = HttpContext.Session.GetInt32(Service.LyyCMSConsts.USER_ID);
                    string uname = HttpContext.Session.GetString(Service.LyyCMSConsts.ADMIN_NAME);



                    //TODO cookie 记录下用户登录信息
                    #region 如果 RemberMe ， 需要使用cookie 记录下来
                    if (loginModel.RemberMe)
                    {
                        
                    }
                    #endregion

                    return RedirectToAction(nameof(Index), "Admin");
                }
            }
            else
            {                
                ModelState.AddModelError("", $"无法登录,账号或密码错误");
                return RedirectToAction("Index", new { LoginModel = loginModel, saveChangesError = true });
            }

        }
    }
}